package com.vviton.zyhj.consumer.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.vviton.zyhj.common.base.AppConfig;
import com.vviton.zyhj.common.base.BaseService;
import com.vviton.zyhj.common.common.CodeConstants;
import com.vviton.zyhj.common.common.ExcelConstants;
import com.vviton.zyhj.common.common.ValidatorConstants;
import com.vviton.zyhj.common.dao.TYyxtCodeMapper;
import com.vviton.zyhj.common.dao.TYyxtDdglDhjlbMapper;
import com.vviton.zyhj.common.entity.TYyxtCode;
import com.vviton.zyhj.common.utils.DateUtils;
import com.vviton.zyhj.common.utils.ExcelUtil;
import com.vviton.zyhj.common.utils.ResultJson;
import com.vviton.zyhj.common.utils.page.Page;
import com.vviton.zyhj.common.view.ParamDdcx;
import com.vviton.zyhj.common.view.ViewDdcx;
import com.vviton.zyhj.consumer.service.WdddglService;

/**
 * ClassName: WdddglServiceImpl <br/>
 * Function: 订单管理（消费者）ServiceImpl. <br/>
 * Date: 2016年11月15日 下午1:34:07 <br/>
 *
 * @author Dengshiyu
 * @version 1.0
 * @since JDK 1.7
 */
public class WdddglServiceImpl extends BaseService implements WdddglService {

    /** 日志 */
    private static Logger logger = Logger.getLogger(WdddglServiceImpl.class);

    /** 读取配置文件信息 */
    @Autowired
    private AppConfig appConfig;

    /** 兑换记录表Mapper */
    @Autowired
    private TYyxtDdglDhjlbMapper tYyxtDdglDhjlbMapper;

    /** 码表Mapper */
    @Autowired
    private TYyxtCodeMapper tYyxtCodeMapper;

    /**
     * getDdcxPagination,根据各种条件查询订单. <br/>
     * Author: Dengshiyu <br/>
     * Create Date: 2016年11月15日 <br/>
     * ===============================================================<br/>
     * Modifier: Dengshiyu <br/>
     * Modify Date: 2016年11月15日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param paramDdcx 订单查询参数
     * @return Page
     * @since JDK 1.7
     */
    @Override
    public Page getDdcxPagination(ParamDdcx paramDdcx, String zh) {
        Page page = new Page();
        if (StringUtils.isBlank(paramDdcx.getYwy()) || StringUtils.isBlank(zh)) {
            logger.info("订单查询业务域和提供方帐号不能为空");
            return page;
        }
        paramDdcx.setTgflx(CodeConstants.TGFLX_YH);
        paramDdcx.setTgfzh(zh);

        page.setParam(paramDdcx);
        Integer total = tYyxtDdglDhjlbMapper.getDdcxTotalPagination(page);
        page.setTotal(total);
        // 分页查询数据
        page.setPageSize(paramDdcx.getPageSize());
        page.setCurrPage(paramDdcx.getCurrPage());
        if (total > 0) {
            List<ViewDdcx> viewDdcxList = tYyxtDdglDhjlbMapper.getDdcxPagination(page);
            // 字段解析
            Map<String, TYyxtCode> dhlbMap = this.getCodeMapCodeclass(CodeConstants.DHLB);
            Map<String, TYyxtCode> dhztMap = this.getCodeMapCodeclass(CodeConstants.DHZT);
            TYyxtCode tYyxtCode = null;
            for (ViewDdcx viewDdcx : viewDdcxList) {
                tYyxtCode = dhlbMap.get(CodeConstants.DHLB + viewDdcx.getDdlb());
                if (tYyxtCode != null) {
                    viewDdcx.setDdlbDesc(tYyxtCode.getCodedesc());
                }
                tYyxtCode = dhztMap.get(CodeConstants.DHZT + viewDdcx.getZt());
                if (tYyxtCode != null) {
                    viewDdcx.setZtDesc(tYyxtCode.getCodedesc());
                }
            }
            page.setData(viewDdcxList);
        }
        return page;
    }

    /**
     * pldcDdxx,批量导出订单信息. <br/>
     * Author: Dengshiyu <br/>
     * Create Date: 2016年11月24日 <br/>
     * ===============================================================<br/>
     * Modifier: Dengshiyu <br/>
     * Modify Date: 2016年11月24日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param ddids 订单id
     * @param ywy 业务域
     * @param res HttpServletResponse
     * @return 操作结果
     * @since JDK 1.7
     */
    @Override
    public ResultJson pldcDdxx(String ddids, String ywy, HttpServletResponse res) {
        ResultJson result = new ResultJson(false);
        if (StringUtils.isBlank(ywy)) {
            result.setMessage(ValidatorConstants.E1501);
            return result;
        }
        if (StringUtils.isBlank(ddids)) {
            result.setMessage(ValidatorConstants.E1502);
            return result;
        }
        String[] ddidArr = ddids.split(",");
        List<ViewDdcx> viewDdcxList = tYyxtDdglDhjlbMapper.getDdcxListByIds(ddidArr, ywy);
        // 字段解析
        Map<String, TYyxtCode> dhlbMap = this.getCodeMapCodeclass(CodeConstants.DHLB);
        Map<String, TYyxtCode> dhztMap = this.getCodeMapCodeclass(CodeConstants.DHZT);
        TYyxtCode tYyxtCode = null;
        for (ViewDdcx viewDdcx : viewDdcxList) {
            tYyxtCode = dhlbMap.get(CodeConstants.DHLB + viewDdcx.getDdlb());
            if (tYyxtCode != null) {
                viewDdcx.setDdlbDesc(tYyxtCode.getCodedesc());
            }
            tYyxtCode = dhztMap.get(CodeConstants.DHZT + viewDdcx.getZt());
            if (tYyxtCode != null) {
                viewDdcx.setZtDesc(tYyxtCode.getCodedesc());
            }
            viewDdcx.setDhsjFormat(DateUtils.format(viewDdcx.getDhsj(), DateUtils.PATTERN_DATETIME));
        }
        if (CollectionUtils.isNotEmpty(viewDdcxList)) {
            if (StringUtils.isNotBlank(appConfig.getLang())) {
                ExcelUtil.exportTemplate(res, ExcelConstants.XFZDDGL_TEMPLATE_PATH, viewDdcxList, ExcelConstants.XFZDDGL_FILENAME_EN);
            } else {
                ExcelUtil.exportTemplate(res, ExcelConstants.XFZDDGL_TEMPLATE_PATH, viewDdcxList, ExcelConstants.XFZDDGL_FILENAME);
            }

            result.setResult(true);
        }
        return result;
    }

}
